﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Workflow.IService;
using Workflow.IService.Menu;
using Workflow.Model;
using Workflow.Model.Enum;
using Workflow.Service.Menu;
using System.Diagnostics;

namespace Workflow.Service
{
    public class UserTree : IUserTree
    {
        public IRoleMenu AdminRoleMenu { get; set; }
        public IRoleMenu UserRoleMenu { get; set; }
        public IRoleMenu GPPLRoleMenu { get; set; }
        public IRoleMenu GPEEMPARoleMenu { get; set; }
        public IRoleMenu GPOMRoleMenu { get; set; }
        public IRoleMenu MSDEEMManagerRoleMenu { get; set; }
        public IRoleMenu MSDEEMOperatorRoleMenu { get; set; }
        public IRoleMenu LimitedRoleMenu { get; set; }
        public IMenuModelsService MenuModelsService { get; set; }

        public Model.JsonMenus GetUserTree(int groupcode, int rolecode)
        {
            #region UserTree by db
            //SaveCodeMenuData();

            //return new JsonMenus(MenuModelsService.GetMenuModelsByRole(rolecode)); 
            #endregion
            
            //GPPTP = 1,
            //MSDPTP = 2,
            //GPEEM=3,
            //MSDEEM=4

            //1	PA	1
            //2	PL	1
            //3	OM	1
            //4	ADMIN	1
            //5	Manager	2
            //6	Operator	2
            //7	PA	3
            //8	PL	3
            //9	OM	3
            //10	ADMIN	3
           
            if (
                rolecode == (int)EnumRole.GPEEMPL )
            {
                JsonMenus jsonMenus = GPPLRoleMenu.GetJsonMenu();
                return jsonMenus;
            }
            if (
                rolecode == (int)EnumRole.GPEEMOM)
            {
                JsonMenus jsonMenus = GPOMRoleMenu.GetJsonMenu();
                return jsonMenus;
            }
            if (rolecode == (int)EnumRole.GPEEMADMIN )
            {
                JsonMenus jsonMenus = AdminRoleMenu.GetJsonMenu();
                return jsonMenus;
            }
            if (rolecode == (int)EnumRole.GPEEMPA)
            {
                JsonMenus jsonMenus = GPEEMPARoleMenu.GetJsonMenu();
                return jsonMenus;
            }
          
            if (rolecode == (int)EnumRole.MSDEEMManager)
            {
                JsonMenus jsonMenus = MSDEEMManagerRoleMenu.GetJsonMenu();
                return jsonMenus;
            }
           
            if (rolecode == (int)EnumRole.MSDEEMOperator)
            {
                JsonMenus jsonMenus = MSDEEMOperatorRoleMenu.GetJsonMenu();
                return jsonMenus;
            }
			else
            {
                JsonMenus jsonMenus = LimitedRoleMenu.GetJsonMenu();
                return jsonMenus;
            }
        }

        #region UserTree by db
		//private void SaveCodeMenuData()
		//{
		//    IList<MenuModels> resultList = GetCodeMenuData();
		//    foreach (var result in resultList)
		//    {
		//        MenuModelsService.SaveMenuModels(result);
		//    }
		//}

		//private IList<MenuModels> GetCodeMenuData()
		//{
		//    IList<MenuModels> resultList = new List<MenuModels>();
		//    TreeToList(GPPTPPARoleMenu.GetJsonMenu().menus, ((int)EnumRole.GPPTPPA), ref resultList);

		//    TreeToList(GPPLRoleMenu.GetJsonMenu().menus, (int)EnumRole.GPPTPPL, ref resultList);
		//    TreeToList(GPPLRoleMenu.GetJsonMenu().menus, (int)EnumRole.GPEEMPL, ref resultList);
		//    TreeToList(GPPLRoleMenu.GetJsonMenu().menus, (int)EnumRole.GPMDMPL, ref resultList);
		//    TreeToList(GPPLRoleMenu.GetJsonMenu().menus, (int)EnumRole.GPAHPTPPL, ref resultList);
		//    TreeToList(GPPLRoleMenu.GetJsonMenu().menus, (int)EnumRole.GPAHEEMPL, ref resultList);
		//    TreeToList(GPPLRoleMenu.GetJsonMenu().menus, (int)EnumRole.GPMDMHCPPL, ref resultList);
		//    TreeToList(GPPLRoleMenu.GetJsonMenu().menus, (int)EnumRole.GPRTRPL, ref resultList);


		//    TreeToList(AdminRoleMenu.GetJsonMenu().menus, (int)EnumRole.GPPTPOM, ref resultList);
		//    TreeToList(AdminRoleMenu.GetJsonMenu().menus, (int)EnumRole.GPEEMOM, ref resultList);
		//    TreeToList(AdminRoleMenu.GetJsonMenu().menus, (int)EnumRole.GPMDMOM, ref resultList);
		//    TreeToList(AdminRoleMenu.GetJsonMenu().menus, (int)EnumRole.GPAHPTPOM, ref resultList);
		//    TreeToList(AdminRoleMenu.GetJsonMenu().menus, (int)EnumRole.GPAHEEMOM, ref resultList);
		//    TreeToList(AdminRoleMenu.GetJsonMenu().menus, (int)EnumRole.GPMDMHCPOM, ref resultList);
		//    TreeToList(AdminRoleMenu.GetJsonMenu().menus, (int)EnumRole.GPRTROM, ref resultList);

		//    TreeToList(AdminRoleMenu.GetJsonMenu().menus, (int)EnumRole.GPEEMADMIN, ref resultList);
		//    TreeToList(AdminRoleMenu.GetJsonMenu().menus, (int)EnumRole.GPPTPADMIN, ref resultList);
		//    TreeToList(AdminRoleMenu.GetJsonMenu().menus, (int)EnumRole.GPMDMADMIN, ref resultList);
		//    TreeToList(AdminRoleMenu.GetJsonMenu().menus, (int)EnumRole.GPAHEEMADMIN, ref resultList);
		//    TreeToList(AdminRoleMenu.GetJsonMenu().menus, (int)EnumRole.GPAHPTPADMIN, ref resultList);
		//    TreeToList(AdminRoleMenu.GetJsonMenu().menus, (int)EnumRole.GPMDMHCPADMIN, ref resultList);
		//    TreeToList(AdminRoleMenu.GetJsonMenu().menus, (int)EnumRole.GPRTRADMIN, ref resultList);

		//    TreeToList(GPEEMPARoleMenu.GetJsonMenu().menus, (int)EnumRole.GPEEMPA, ref resultList);

		//    TreeToList(GPMDMPARoleMenu.GetJsonMenu().menus, (int)EnumRole.GPMDMPA, ref resultList);

		//    TreeToList(GPMDMHCPPARoleMenu.GetJsonMenu().menus, (int)EnumRole.GPMDMHCPPA, ref resultList);

		//    TreeToList(GPRTRPARoleMenu.GetJsonMenu().menus, (int)EnumRole.GPRTRPA, ref resultList);

		//    TreeToList(GPAHPTPPARoleMenu.GetJsonMenu().menus, (int)EnumRole.GPAHPTPPA, ref resultList);

		//    TreeToList(GPAHEEMPARoleMenu.GetJsonMenu().menus, (int)EnumRole.GPAHEEMPA, ref resultList);

		//    TreeToList(MSDEEMManagerRoleMenu.GetJsonMenu().menus, (int)EnumRole.MSDEEMManager, ref resultList);

		//    TreeToList(MSDPTPManagerRoleMenu.GetJsonMenu().menus, (int)EnumRole.MSDPTPManager, ref resultList);

		//    TreeToList(MSDPTPOperatorRoleMenu.GetJsonMenu().menus, (int)EnumRole.MSDPTPOperator, ref resultList);

		//    TreeToList(MSDEEMOperatorRoleMenu.GetJsonMenu().menus, (int)EnumRole.MSDEEMOperator, ref resultList);

		//    TreeToList(MSDMDMOperatorRoleMenu.GetJsonMenu().menus, (int)EnumRole.MSDMDMOperator, ref resultList);

		//    TreeToList(MSDMDMManagerRoleMenu.GetJsonMenu().menus, (int)EnumRole.MSDMDMManager, ref resultList);

		//    TreeToList(MSDMDMHCPOperatorRoleMenu.GetJsonMenu().menus, (int)EnumRole.MSDMDMHCPOperator, ref resultList);

		//    TreeToList(MSDMDMHCPManagerRoleMenu.GetJsonMenu().menus, (int)EnumRole.MSDMDMHCPManager, ref resultList);

		//    TreeToList(MSDRTROperatorRoleMenu.GetJsonMenu().menus, (int)EnumRole.MSDRTROperator, ref resultList);

		//    TreeToList(MSDRTRManagerRoleMenu.GetJsonMenu().menus, (int)EnumRole.MSDRTRManager, ref resultList);

		//    TreeToList(MSDAHPTPManagerRoleMenu.GetJsonMenu().menus, (int)EnumRole.MSDAHPTPManager, ref resultList);

		//    TreeToList(MSDAHEEMManagerRoleMenu.GetJsonMenu().menus, (int)EnumRole.MSDAHEEMManager, ref resultList);

		//    TreeToList(MSDAHPTPOperatorRoleMenu.GetJsonMenu().menus, (int)EnumRole.MSDAHPTPOperator, ref resultList);

		//    TreeToList(MSDAHEEMOperatorRoleMenu.GetJsonMenu().menus, (int)EnumRole.MSDAHEEMOperator, ref resultList);

		//    TreeToList(LimitedRoleMenu.GetJsonMenu().menus, -1, ref resultList);
		//    return resultList;
		//}

		//private void TreeToList(IList<MenuModels> sourceList, int rolecode, ref IList<MenuModels> resultList)
		//{
		//    if (sourceList == null || sourceList.Count == 0)
		//    {
		//        return;
		//    }
		//    if (resultList == null)
		//    {
		//        resultList = new List<MenuModels>();
		//    }
		//    foreach (var m in sourceList)
		//    {
		//        var rm = resultList.FirstOrDefault(o => o.menuid == m.menuid);
		//        if (rm == null)
		//        {
		//            m.Roles = "," + rolecode + ",";
		//            resultList.Add(m);
		//        }
		//        else
		//        {
		//            if (!rm.Roles.Contains("," + rolecode + ","))
		//            {
		//                rm.Roles += rolecode + ",";
		//            }
		//        }

		//        if (m.menus != null && m.menus.Count > 0)
		//        {
		//            foreach (var item in m.menus)
		//            {
		//                item.ParentId = Convert.ToInt32(m.menuid);
		//                item.ParentId = item.ParentId <= 0 ? null : item.ParentId;
		//            }
		//            TreeToList(m.menus, rolecode, ref resultList);
		//        }
		//    }
		//} 
        #endregion
    }
}
